<?php

namespace app\admin\controller;

use think\facade\Db;
use app\model\DailyDeliveryData;
use app\model\DeliveryData;

class Repair
{
    // 修复数据
    public function data()
    {
        $saveData = $saveDeliveryData = [];
        for ($i = 0; $i <= 30; $i++) {
            $date = date('Ymd', 1696089600 + $i * 86400);

            $costData = Db::table('history_cost')->where(['date' => $date])->select()->toArray();

            $daily_delivery_data = Db::name('daily_delivery_data')->where(['date' => $date, 'platform' => 1])->select()->toArray();

            $linkIds = array_column($daily_delivery_data, 'link_id');

            $linkData = Db::name('link')->where(['id' => $linkIds])->select()->toArray();

            $linkAdvertiserIds = array_column($linkData, 'advertiser_id', 'id');
            $linkRebate = array_column($linkData, 'rebate', 'id');

            $cost = array_column($costData, NULL, 'id');

            $deliveryCost = $deliveryActualCost = $totalRecharge = 0;

            $rechargeData = Db::name('pay_record')->where(['link_id' => $linkIds, 'status' => 1, 'activation_date' => $date])->field('link_id, total_fee')->select()->toArray();

            $recharge = [];
            foreach ($rechargeData as $k => $item) {
                $recharge[$item['link_id']][] = $item['total_fee'];
            }

            foreach ($daily_delivery_data as $key => $value) {
                $advertiserId = $linkAdvertiserIds[$value['link_id']];
                if (empty($cost[$advertiserId])) {
                    file_put_contents('./not_advertiser_id_data.txt', $date.' ：'.$advertiserId.' ：'.$value['link_id'].';'.PHP_EOL, FILE_APPEND);
                    Db::name('daily_delivery_data')->where(['id' => $value['link_id']])->delete();
                    continue;
                }

                $juliangCost = $cost[$advertiserId];

                $value['cost'] = round($juliangCost['non_grant_cost'] + $juliangCost['return_goods_wallet_cost'], 2);
                $deliveryCost += $value['cost'];

                $rebate = $linkRebate[$value['link_id']];
                $value['actual_cost'] = round($value['cost'] / ($rebate / 100 + 1), 2);
                $deliveryActualCost += $value['actual_cost'];

                $linkRecharge = empty($recharge[$value['link_id']]) ? 0 : round(array_sum($recharge[$value['link_id']]), 2);

                $saveData[] = [
                    'id' => $value['id'],
                    'date' => $date,
                    'cost' => $value['cost'],
                    'actual_cost' => $value['actual_cost'],
                    'total_recharge' => $linkRecharge
                ];

                $totalRecharge += $linkRecharge;
            }

            $delivery_data = Db::name('delivery_data')->where(['date' => $date, 'platform' => 1])->find();
            $saveDeliveryData[] = [
                'id' => $delivery_data['id'],
                'total_cost' => round($deliveryCost, 2),
                'actual_cost' => round($deliveryActualCost, 2),
                'total_recharge' => round($totalRecharge, 2)
            ];
        }

        // dump($saveData);
        // dump($saveDeliveryData);die;

        $model = new DailyDeliveryData();
        $bool = $model->saveAll($saveData);

        $deliveryDataModel = new DeliveryData();
        $delivery_data_bool = $deliveryDataModel->saveAll($saveDeliveryData);

        dump($bool);
        dump($delivery_data_bool);
        die;
    }
}